package com.aracky.system.controller;

import com.aracky.common.annotation.DataDeal;
import com.aracky.common.util.ComUtil;
import com.aracky.common.util.ConvertUtil;
import com.aracky.common.vo.CommonResult;
import com.aracky.common.vo.PageObjectVO;
import com.aracky.system.model.entity.SysMenuTable;
import com.aracky.system.model.form.SysMenuTableForms;
import com.aracky.system.model.request.SysMenuTableRequest;
import com.aracky.system.model.vo.SysBtnConfigVO;
import com.aracky.system.model.vo.SysMenuTableListVO;
import com.aracky.system.model.vo.SysMenuTableVO;
import com.aracky.system.model.vo.view.ConfigVO;
import com.aracky.system.service.SysBtnConfigService;
import com.aracky.system.service.SysMenuTableService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 菜单和数据模型配置关系表 前端控制器
 * </p>
 *
 * @author zengzhiping
 * @since 2022-07-19 10:22:14
 */
@RestController
@RequestMapping("/sysMenuTable")
@Api(tags = "菜单与数据模型关联相关接口")
public class SysMenuTableController {
    @Autowired
    private SysMenuTableService sysMenuTableService;

    @Autowired
    private SysBtnConfigService sysBtnConfigService;

    @PostMapping("/saveOrUpdateMenuTable")
    @ApiOperation(value = "新增或修改菜单与数据模型关联关系")
    public CommonResult saveOrUpdateSysMenuTable(@RequestBody SysMenuTableForms sysMenuTableForms) {
        sysMenuTableService.saveOrUpdateSysMenuTable(sysMenuTableForms);
        return CommonResult.success("数据模型配置关联成功");
    }

    @DeleteMapping("/deleteMenuTable")
    @ApiOperation(value = "删除菜单与数据模型关联关系")
    @ApiImplicitParam(name = "mtIds", value = "sys_menu_table表id集合")
    public CommonResult deleteSysMenuTable(@RequestParam(required = false) List<Long> mtIds) {
        if (ComUtil.isNullList(mtIds)) {
            return CommonResult.error("删除表id不能为空");
        }
        sysMenuTableService.deleteSysMenuTable(mtIds);
        return CommonResult.success("菜单与数据模型关联删除成功");
    }

    @DataDeal(user = true, dict = true)
    @PostMapping("/getMenuTableList")
    @ApiOperation(value = "通过菜单id查询数据模型配置")
    public CommonResult<PageObjectVO<SysMenuTableVO>> getTableMenuList(@RequestBody SysMenuTableRequest sysMenuTableRequest) {
        PageObjectVO<SysMenuTableVO> pageObjectVO = sysMenuTableService.getMenuTablePage(sysMenuTableRequest);
        return CommonResult.success(pageObjectVO);
    }

    @GetMapping("/getConfig")
    @ApiOperation(value = "通过菜单id查询数据模型配置和按钮配置信息")
    @ApiImplicitParam(name = "menuId", value = "菜单id")
    public CommonResult<ConfigVO> getPageConfig(Long menuId) {
        //查询当前菜单下的所以配置信息
        LambdaQueryWrapper<SysMenuTable> tableLambdaQueryWrapper =
                Wrappers.<SysMenuTable>lambdaQuery().eq(SysMenuTable::getMenuId, menuId);
        List<SysMenuTable> sysMenuTableList = sysMenuTableService.list(tableLambdaQueryWrapper);
        List<SysMenuTableListVO> sysMenuTableVOList = ConvertUtil.sourceToTargets(sysMenuTableList, SysMenuTableListVO.class);

        //查询所有按钮配置信息
        List<SysBtnConfigVO> sysBtnConfigVOList = sysBtnConfigService.getBtnCongifList(Collections.singletonList(menuId));
        ConfigVO configVO = new ConfigVO();
        configVO.setBtnConfig(sysBtnConfigVOList);
        configVO.setPageConfig(sysMenuTableVOList);
        return CommonResult.success(configVO);
    }


}
